package com.bdqn.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.RoleService;
import com.bdqn.service.UserService;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.authc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private UserService service;
    @Resource
    private RoleService roleService;

    @GetMapping("toLogin")
    public String toLogin() {

        return "login";
    }

    @PostMapping("doLogin")
    public String doLogin(Model model, String usrName, String usrPassword, HttpSession session) {
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(usrName, usrPassword);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);//认证、登录
            //认证(登录)成功
            User user = (User) subject.getPrincipal();
            session.setAttribute("loginUser", user);
            return "redirect:/main";//建议重定向
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            model.addAttribute("msg", "用户名或密码错误，登录失败！");
            return "login";
        } catch (LockedAccountException e) {
            model.addAttribute("msg", "用户被禁用，登录失败！");
            return "login";
        } catch (AuthenticationException e) {
            model.addAttribute("msg", "认证异常，登录失败！");
            return "login";
        }
    }

    @RequestMapping("user/list")
    public String toList(Model model, String usrName, Long roleId, @RequestParam(defaultValue = "1") Long pageIndex) {
        List<Role> role = roleService.list();
        Page page = new Page();
        page.setCurrent(pageIndex);
        User user = new User();
        user.setUsrName(usrName);
        user.setUsrRoleId(roleId);
        IPage iPage = service.getPage(page, user);
        model.addAttribute("roles", role);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        model.addAttribute("userPager", iPage);
        return "user/list";
    }

    @GetMapping("user/add")
    public String toAdd(Model model) {
        List<Role> role = roleService.list();
        model.addAttribute("roles", role);
        return "user/add";
    }

    @PostMapping("user/doAdd")
    public String doAdd(User user) {
        user.setUsrPassword(service.save(user.getUsrPassword()));
        boolean bo = service.save(user);
        if (bo) {
            return "redirect:list";
        }
        return "user/add";
    }

    @GetMapping("user/edit")
    public String toEdit(Model model, Integer usrId) {
        User user = service.getById(usrId);
        List<Role> role = roleService.list();
        model.addAttribute("roles", role);
        model.addAttribute("sysUser", user);
        return "user/edit";
    }

    @PostMapping("user/save")
    public String doSave(User user) {
        user.setUsrPassword(service.save(user.getUsrPassword()));
        boolean bo = service.updateById(user);
        if (bo) {
            return "redirect:list";
        }
        return "user/edit";
    }

    @GetMapping("user/del/{usrId}")
    @ResponseBody
    public Map doDel(@PathVariable("usrId") Integer usrId) {
        boolean bo = service.removeById(usrId);
        Map map = new HashMap();
        if (bo) {
            map.put("result", "true");
        }
        return map;
    }
}
